package cn.wsalix.admin.repository;

import java.util.List;

import javax.persistence.QueryHint;

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.jpa.repository.QueryHints;
import org.springframework.data.repository.query.Param;

import cn.anlaser.repository.RelatedRepository;
import cn.wsalix.admin.entity.SysRole;
import cn.wsalix.admin.entity.SysUser;
import cn.wsalix.admin.entity.UserRole;
import cn.wsalix.admin.form.UserRoleForm;

public interface UserRoleRepository extends RelatedRepository<UserRoleForm,UserRole,SysUser,SysRole> {

	@Query(value = "select ur from UserRole ur")
	@QueryHints({ @QueryHint(name = "org.hibernate.cacheable", value = "true") })
	public List<UserRole> findAllWithCache();

	@Query("select r from UserRole ur,SysRole r where ur.postFix.id=r.id and ur.preFix.username = :username")
	public List<SysRole> findRolesByUserName(@Param("username") String username);

	@Query("select ur from UserRole ur where ur.postFix.id=:roleId and ur.preFix.id = :userId")
	public List<UserRole> findUserAndRole(@Param("userId") Long userId,
			@Param("roleId") Long roleId);
	
	@Query("select u from UserRole ur,SysRole u where ur.postFix.id = u.id and ur.preFix.id=:userId")
	public List<SysRole> findRolesByUserId(@Param("userId") Long userId);

	@Query("select u from UserRole ur,SysUser u where ur.preFix.id = u.id and ur.postFix.id=:roleId")
	public List<SysUser> findUsersByRoleId(@Param("roleId") Long roleId);
	
	//
	@Query("select ur from UserRole ur where ur.preFix.id = :userId")
	public List<UserRole> findByUserId(@Param("userId") Long userId);
	
	@Query("select ur from UserRole ur where ur.postFix.id = :roleId")
	public List<UserRole> findByRoleId(@Param("roleId") Long roleId);
}
